package com.sx.wechat.controller.teacher;

import com.alibaba.druid.sql.PagerUtils;
import com.alibaba.druid.util.JdbcConstants;
import com.alibaba.fastjson.JSONObject;
import com.sx.core.dao.InformationMapper;
import com.sx.core.dao.LessonMapper;
import com.sx.core.model.Information;
import com.sx.core.utils.UUIDUtils;
import com.sx.core.vo.JsonReaderResponse;
import com.sx.wechat.controller.BaseController;
import com.sx.wechat.security.AuthenticationHolder;
import com.sx.wechat.shiro.InformationPrincipal;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by lihao on 2017/6/29.
 */
@RequestMapping("tc/lesson")
@RestController
public class LessonController extends BaseController {
    @Autowired
    LessonMapper lessonMapper;
    @Autowired
    InformationMapper informationMapper;

    @RequestMapping("list_user_lesson")
    public JsonReaderResponse list_lesson(@RequestBody JSONObject params) {
        InformationPrincipal information= AuthenticationHolder.getCurrentInfo();
        int page= NumberUtils.toInt(ObjectUtils.toString(params.get("page")),1);
        int limit= NumberUtils.toInt(ObjectUtils.toString(params.get("limit")),25);
        int type=params.getInteger("type");
        if(type==0){
            params.put("teacherid",information.getId());
        }

        params.put("start",(page-1)*limit);

        String sql="select l.id,l.name,l.creator,l.yy,l.createdate,l.icon,i.name as teachername from t_lesson l JOIN t_information i on l.creator = i.id WHERE 1=1 ";
        if(type==0){
            sql+=" AND l.creator=:teacherid ";
        }
        String keyword=params.getString("keyword");
        if(StringUtils.isNotBlank(keyword)){
            sql+=" AND (l.name LIKE  CONCAT('%',:keyword,'%') OR l.content LIKE  CONCAT('%',:keyword,'%'))";
        }

        String count_sql= PagerUtils.count(sql, JdbcConstants.MYSQL);
        long count = namedParameterJdbcTemplate.queryForObject(count_sql,params,Long.class);

        sql+=" ORDER BY l.createdate DESC limit :start,:limit";

        List<Map<String,Object>> list=namedParameterJdbcTemplate.queryForList(sql,params);
        return new JsonReaderResponse(list,count,page,limit).addExtra(informationMapper.get(Information.class,information.getId()));
    }
    @RequestMapping("add_user_lesson")
    public JsonReaderResponse list_student(@RequestBody JSONObject params) {
        InformationPrincipal information= AuthenticationHolder.getCurrentInfo();
        params.put("id", UUIDUtils.generateUuid32());
        params.put("createdate", new Date());
        params.put("content",params.getString("content").replaceAll("\n\r",""));
        params.put("files",params.getJSONArray("files")!=null?params.getJSONArray("files").toJSONString():null);
        params.put("creator",information.getId());
        params.put("icon",null);
        namedParameterJdbcTemplate.update("insert into t_lesson (id,name,content,creator,createdate,files,icon) values (:id,:name,:content,:creator,:createdate,:files,:icon);",params);
        return new JsonReaderResponse();
    }
    @RequestMapping("update_user_lesson")
    public JsonReaderResponse update_user_lesson(@RequestBody JSONObject params) {
        params.put("content",params.getString("content").replaceAll("\n\r",""));
        params.put("files",params.getJSONArray("files")!=null?params.getJSONArray("files").toJSONString():null);
        namedParameterJdbcTemplate.update("UPDATE t_lesson SET name=:name,content=:content,yy=:yy,files=:files,icon=:icon WHERE id=:id",params);
        return new JsonReaderResponse();
    }
    @RequestMapping("delete_user_lesson")
    public JsonReaderResponse delete_user_lesson(@RequestBody JSONObject params) {
        namedParameterJdbcTemplate.update("DELETE FROM t_lesson WHERE id=:id",params);
        return new JsonReaderResponse();
    }
    @RequestMapping("get_user_lesson/{id}")
    public JsonReaderResponse get_user_lesson(@PathVariable String id) {
        return new JsonReaderResponse().addExtra(jdbcTemplate.queryForMap("SELECT  * FROM t_lesson WHERE id=?",id));
    }












}
